import { Component } from "@angular/core";
import { NavController,App,AlertController,NavParams } from 'ionic-angular';

import { AppConfig } from '../../../../app/app.config';

import { HttpClient,HttpHeaders, } from '@angular/common/http';
import { NativeStorage } from '@ionic-native/native-storage';
@Component({
    selector: 'page-reset',
    templateUrl: 'name.html',
})
export class NamePage {
    info = {
        firstname:'',
    }
    pushData:any
    verify_time=0
    token:any
    constructor(
        public navCtrl: NavController,
        public app:App,
        public alertCtrl:AlertController,
        public http:HttpClient,
        private storage:NativeStorage,
        private navParams:NavParams
    ) {
        this.storage.getItem('token').then(token=>{
            this.token = token;
        })
        this.pushData = this.navParams.get('info');
        this.info.firstname = this.pushData.firstname;
    }
    alertb(title,subTitle){
        let alert = this.alertCtrl.create({
            title: title,
            subTitle: subTitle,
            buttons: ['OK']
        });
        alert.present();
    }
    goResetPwd(){
        this.http.post(AppConfig.getUrl()+'user/edit?token='+this.token, AppConfig.urlEcode(this.info),{headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' }) })
            .subscribe( (res) => {
                if(res['code']===101){
                    this.alertb('success','Modify name is successful!');
                    let callback = this.navParams.get('callback');
                    callback({key:'firstname',value:this.info.firstname})
                    this.navCtrl.pop();
                }else if(res['code']===202){
                    this.alertb('Error!',res['msg']);
                }
            })
    }
    
}